package com.hotcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/25 9:52
 */
public class leecode3 {
    public static int lengthOfLongestSubstring(String s) {
        if (s.length() < 2) {
            return s.length();
        }
        Map<Character, Integer> map = new HashMap<>();
        int res = 0;
        for (int start = 0, end = 0; end < s.length(); end++) {
            if (map.containsKey(s.charAt(end))) {
                start = Math.max(start, map.get(s.charAt(end)));
            }
            //避免去-1
            map.put(s.charAt(end), end + 1);
            res = Math.max(res, end - start + 1);
        }
        return res;
    }


    public static int way2(String s) {
        if (s.length() < 2) {
            return s.length();
        }
        int[] chars = new int[256];
        int left = 0;
        int right = 0;
        int res = 0;
        while (right < s.length()) {
            chars[s.charAt(right)]++;
            while (chars[s.charAt(right)] > 1) {
                chars[s.charAt(left)]--;
                left++;
            }
            res = Math.max(res, right - left + 1);

            right++;
        }
        return Math.max(res, 0);

    }





    public static void main(String[] args) {
        String s = "abcabcbb";
        System.out.println(way2(s));
    }
}
